import Vue from 'vue'

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import locale from 'element-ui/lib/locale/lang/zh-CN' // lang i18n 使用中文

import './utils/date/index';
import './utils/string/index';
import './assets/css/base.scss'
import App from './App'
import router from './router'
import store from './store'
import axios from './plugins/axios'
import VueCropper from 'vue-cropper'
Vue.use(VueCropper);
Vue.use(ElementUI, {locale});
Vue.config.productionTip = false;
// http://182.61.15.30:8400/
// http://192.168.2.148:8400 本地
// https://www.smartptm.com/ptm
Vue.prototype.serverIp = process.env.NODE_ENV === 'production' ? location.origin + '/ptm/' : 'https://www.smartptm.com/ptm';
Vue.prototype.$http = axios;
Vue.prototype.upload_file_max_size = 20971520;//2M


//将base64转换为文件，dataurl为base64字符串，filename为文件名（必须带后缀名，如.jpg,.png）
Vue.prototype.dataURLtoFile = function(dataurl, filename) {
  var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
  bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
  while(n--){
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new File([u8arr], filename, {type:mime});
};

//文件转base64
Vue.prototype.blobToBase64 = function(blob, callback) {
  let a = new FileReader();
  a.onload = (e) =>{ callback(e.target.result); };
  a.readAsDataURL(blob);
};


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: {App},
  template: '<App/>'
});


router.beforeEach((to, from, next) => {
  console.log(store);
  // console.log(to,"toooo")
  // console.log(from,"from")
  if (from.meta.isRouter == true&&store.state.setMilePostSave==false) {
    // history.pushState(null, null, location.href)
    next({path:from.fullPath,replace:true});
    Vue.prototype.$confirm('里程碑还未保存, 是否退出?', '提示', {
      confirmButtonText: '退出',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      next();
    }).catch(() => {
      next({path:from.fullPath})
    });
  } else {
   next()
  }
});

